package com.jiyinit.web.core.run.dao;

import com.jiyinit.core.framework.dao.mybatis.base.MybatisDaoContext;
import com.jiyinit.web.core.run.dto.RunCountRequest;
import com.jiyinit.web.core.run.entity.TabRunCountEntity;
import com.jiyinit.web.core.run.vo.RunCountVo;
import javafx.scene.control.Tab;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Jiyinuser071 on 2018/12/12.
 */

@Repository("run.tabRunCountDao")
public class TabRunCountDao {
    private String SQLID = "run.tabRunCountDao.";

    @Autowired
    @Qualifier("core.MybatisDaoContext")
    private MybatisDaoContext mybatisDaoContext;

    /**
     * 添加
     * @param entity
     * @return
     */
    public int add(TabRunCountEntity entity){
        return mybatisDaoContext.save(SQLID+"insert",entity);
    }

    /**
     * 根据userId查询是否存在数据
     */
    public List<TabRunCountEntity> findCountById(TabRunCountEntity runCountEntity){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId",runCountEntity.getUserId());
        map.put("dateTime",runCountEntity.getDateTime());
        return mybatisDaoContext.findAll(SQLID+"findCountById",TabRunCountEntity.class,map);
    }

    /**
     * 删除原有数据
     */
    public int deleteRunCount(TabRunCountEntity runCountEntity){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId",runCountEntity.getUserId());
        map.put("dateTime",runCountEntity.getDateTime());
        return mybatisDaoContext.delete(SQLID+"deleteRunCount",map);
    }

    /**
     * 根据userId和dateTime更改数据(如果当天就更改)
     */
    public int updateRunCount(TabRunCountEntity runCountEntity){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("runCountEntity",runCountEntity);
        return mybatisDaoContext.delete(SQLID+"updateRunCount",map);
    }

    /**
     * 查询今日所有用户的步数等信息
     */
    public List<RunCountVo> selectAllByDateTime(String dateTime,int topOrgId){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("dateTime",dateTime);
        map.put("topOrgId",topOrgId);
        return mybatisDaoContext.findAll(SQLID+"selectAllByDateTime",RunCountVo.class,map);
    }

    /**
     * 根据userId查询单条信息
     */
    public RunCountVo selectOneByUserId(Integer selectId,String endDate){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("selectId",selectId);
        map.put("endDate",endDate);
        return mybatisDaoContext.findOne(SQLID+"selectOneByUserId",RunCountVo.class,map);
    }

    /**
     * 查询本周数据
     */
    public List<TabRunCountEntity> findWeekRunCountByUserId(Integer selectId,String startDate,String endDate){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("selectId",selectId);
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        return mybatisDaoContext.findAll(SQLID+"findWeekRunCountByUserId",TabRunCountEntity.class,map);
    }

}
